<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;

class RankService
{
    public $store_key;
    public $redis;
    public $key;

    public function __construct()
    {
        $redis_store = Cache::store('redis');
        $this->redis = $redis_store->getRedis();
        $this->store_key = $redis_store->getPrefix();
    }

    public function setKey($key)
    {
        $this->key = $this->store_key . 'ranking-list:' . $key . ':';
        return $this;
    }

    //返回元素所在表顺序/降序的位置
    public function zrevrank($id)
    {
        $rank = $this->redis->zrevrank($this->key, $id);
        if ($rank !== false) {
            return $rank + 1;
        } else {
            return '--';
        }
    }

    public function __call($method, $parameters)
    {
        array_unshift($parameters, $this->key);
        return $this->redis->__call(strtolower($method), $parameters);
    }
}
